<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    输入你的姓名:<input type="text">
    <p id="pp"></p>
    <script>
        var inp = document.getElementsByTagName('input')[0];
        var pp = document.getElementById('pp');
        inp.oninput = function(){
            var xhr = new XMLHttpRequest();
            xhr.open('get','2-1.php?name='+inp.value,true);
            xhr.send(null);
            xhr.onreadystatechange = function(){
                if(this.readyState == 4){
                    pp.innerHTML = this.responseText == '1'?'error':'ok';
                }
            }
        }
    </script>
</body>
</html>